package com.ceac.kronos.ejb.entitybeans;

import com.ceac.kronos.ejb.entitybeans.Category;

import com.ceac.kronos.ejb.entitybeans.Participant;
import com.ceac.kronos.ejb.entitybeans.Race;

import java.io.Serializable;

import java.sql.Timestamp;

import java.util.Date;
import java.util.List;

import java.util.Vector;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@NamedQueries( { @NamedQuery(name = "Edition.findAll", query = "select o from Edition o"),
        @NamedQuery(name = "Edition.findByCompetition", query = "select o from Edition o where o.competition=:competition"),
        @NamedQuery(name = "Edition.findByNumber", query = "select o from Edition o where o.competition=:competition and o.editionNumber=:editionNumber") })
@Table(name = "EDITIONS")
public class Edition implements Serializable {
    @Column(name="ALTERNATIVE_NAME")
    private String alternativeName;
    @OneToOne(targetEntity = Competition.class)
    @JoinColumn(name = "COMPETITION_ID", referencedColumnName = "COMPETITION_ID")
    private Competition competition;
    @Column(name="CREATED_BY")
    private Integer createdBy;
    @Column(name="CREATION_DATE")
    private Timestamp creationDate;
    @Id
    @Column(name="EDITION_ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.TABLE)
    @TableGenerator(name="EditionGenerator", table="SEQUENCES",
        schema = "kronos", allocationSize=1, initialValue = 1,
                    pkColumnName = "SEQUENCE_NAME", valueColumnName = "SEQUENCE_COUNT", pkColumnValue="DEFAULT")
    private Integer editionId;
    @Column(name="EDITION_NUMBER", nullable = false)
    private Integer editionNumber;
    @Column(name="EVENT_DATE")
    private Timestamp eventDate;
    @Column(name="LAST_UPDATED_BY")
    private String lastUpdatedBy;
    @Column(name="LAST_UPDATE_DATE")
    private Timestamp lastUpdateDate;
    @Column(name="OPEN_DATE")
  private Timestamp openDate;
  @Column(name="CLOSE_DATE")
  private Timestamp closeDate;
    
    @OneToMany(targetEntity = Race.class, mappedBy = "edition", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "EDITION_ID", referencedColumnName = "EDITION_ID")
    private List<Race> races;

    public Edition() {
      this.races = new Vector<Race>();
      this.setCreationDate(new Timestamp((new Date()).getTime()));
      this.setLastUpdateDate(new Timestamp((new Date()).getTime()));
      this.setCreatedBy(0);
      this.setLastUpdatedBy("0");
    }

    public String getAlternativeName() {
        return alternativeName;
    }

    public void setAlternativeName(String alternativeName) {
        this.alternativeName = alternativeName;
    }

    public Integer getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Integer createdBy) {
        this.createdBy = createdBy;
    }

    public Timestamp getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Timestamp creationDate) {
        this.creationDate = creationDate;
    }

    public Integer getEditionId() {
        return editionId;
    }

    public void setEditionId(Integer editionId) {
        this.editionId = editionId;
    }

    public Integer getEditionNumber() {
        return editionNumber;
    }

    public void setEditionNumber(Integer editionNumber) {
        this.editionNumber = editionNumber;
    }

    public Timestamp getEventDate() {
        return eventDate;
    }

    public void setEventDate(Timestamp eventDate) {
        this.eventDate = eventDate;
    }

    public String getLastUpdatedBy() {
        return lastUpdatedBy;
    }

    public void setLastUpdatedBy(String lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }

    public Timestamp getLastUpdateDate() {
        return lastUpdateDate;
    }

    public void setLastUpdateDate(Timestamp lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }

    public void setRaces(List<Race> races) {
        this.races = races;
    }

    public List<Race> getRaces() {
        return races;
    }

    public void setCompetition(Competition competition) {
        this.competition = competition;
    }

    public Competition getCompetition() {
        return competition;
    }

    public void setOpenDate(Timestamp openDate) {
        this.openDate = openDate;
    }

    public Timestamp getOpenDate() {
        return openDate;
    }

    public void setCloseDate(Timestamp closeDate) {
        this.closeDate = closeDate;
    }

    public Timestamp getCloseDate() {
        return closeDate;
    }

    public void addRace(Race r) {
        this.getRaces().add(r);
        r.setEdition(this);
    }
}
